﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BaseData;
using BaseData.Common;
using BaseData.UI;
using TaxEntity.Log;
using TaxEntity.User;

namespace TaxData.Log
{
    /// <summary>
    /// 邮件日志数据层
    /// </summary>
    public class EmailLogData
    {
        /// <summary>
        /// 获取邮件日志
        /// </summary>
        public static DataTable GetList(string strWhere, int pageSize, int page, ref int allCount)
        {
            DataTable dt = new DataTable();
            string condition = string.Empty;
            if (!string.IsNullOrEmpty(strWhere))
            {
                condition = " where 1=1" + strWhere;
            }
            string sqlCount = "select count(1) from Log_SendEmail with(nolock) " + condition;
            allCount = (int)DataConfig.GetSingle(BusinessType.DBRead, sqlCount);
            if (allCount > 0)
            {
                int startNum = (page - 1) * pageSize + 1;
                int endNum = page * pageSize;
                string sqlInner = "select *,ROW_NUMBER() over(order by optTime desc) as RowNumber from Log_SendEmail with(nolock) " + condition;
                string sqlUserList = string.Format("SELECT * FROM ({0}) T WHERE T.RowNumber BETWEEN {1} AND {2} order by RowNumber asc", sqlInner, startNum, endNum);
                dt = DataConfig.QueryRecords(BusinessType.DBRead, sqlUserList);
            }
            return dt;
        }

        /// <summary>
        /// 增加邮件日志
        /// </summary>
        public static long Add(UserEntity sendToUser, string receiveUser, int departmentID, string OptContent, bool isSuccess)
        {
            long id = 0;
            try
            {
                EmailLogEntity model = new EmailLogEntity();
                model.UserID = sendToUser.UserId;
                model.UserName = sendToUser.UserName;
                model.SName = sendToUser.SName;
                model.OptContent = OptContent;
                model.Status = isSuccess ? 1 : 0;
                model.SendUser = "system";
                model.SendToUser = receiveUser;
                model.DepartmentID = departmentID;
                model.OptTime = DateTime.Now;
                id = Add(model);
            }
            catch
            { }
            return id;
        }

        /// <summary>
        /// 增加邮件日志
        /// </summary>
        public static long Add(EmailLogEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.UserID != null)
            {
                strSql1.Append("UserID,");
                strSql2.Append("" + model.UserID + ",");
            }
            if (model.UserName != null)
            {
                strSql1.Append("UserName,");
                strSql2.Append("'" + model.UserName + "',");
            }
            if (model.SName != null)
            {
                strSql1.Append("SName,");
                strSql2.Append("'" + model.SName + "',");
            }
            if (model.OptContent != null)
            {
                strSql1.Append("OptContent,");
                strSql2.Append("'" + model.OptContent.Replace("'", "''") + "',");
            }
            if (model.OptTime != null)
            {
                strSql1.Append("OptTime,");
                strSql2.Append("'" + model.OptTime + "',");
            }
            if (model.DepartmentID != null)
            {
                strSql1.Append("departmentID,");
                strSql2.Append("" + model.DepartmentID + ",");
            }
            if (model.SendUser != null)
            {
                strSql1.Append("SendUser,");
                strSql2.Append("'" + model.SendUser + "',");
            }
            if (model.SendToUser != null)
            {
                strSql1.Append("SendToUser,");
                strSql2.Append("'" + model.SendToUser + "',");
            }
            if (model.Status != null)
            {
                strSql1.Append("Status,");
                strSql2.Append("" + model.Status + ",");
            }
            strSql.Append("insert into Log_SendEmail(");
            strSql.Append(strSql1.ToString().TrimEnd(','));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().TrimEnd(','));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = null;
            try
            {
                DataConfig.GetSingle(BusinessType.DBWrite, strSql.ToString());
            }
            catch
            { }
            long id = 0;
            if (obj != null && obj != DBNull.Value)
            {
                long.TryParse(obj.ToString(), out id);
            }
            return id;
        }
    }
}
